package user

import (
	"context"
	"fmt"

	"golang.org/x/crypto/bcrypt"
)

type UserService interface {
	CreaterUser(context.Context, *CreaterUserRequest) (*User, error)
	DeleterUser(context.Context, *DeleterUserRequest) error
	DescribeUser(context.Context, *DescribeUserRequest) (*User, error)
}

type CreaterUserRequest struct {
	UserName string `json:"username"`
	Password string `json:"password"`
	Role     Role   `json:"role"`
	Ishash   bool
}

func NewCreaterUserRequest() *CreaterUserRequest {
	return &CreaterUserRequest{}
}

func (c *CreaterUserRequest) PasswordHash() {
	if !c.Ishash {
		b, _ := bcrypt.GenerateFromPassword([]byte(c.Password), bcrypt.DefaultCost)
		c.Password = string(b)
		c.Ishash = true
	}
}

type DeleterUserRequest struct {
	ID int
}

func (d *DeleterUserRequest) Idstring() string {
	return fmt.Sprintf("%d", d.ID)
}

func NewDeleterUserRequest(id int) *DeleterUserRequest {
	return &DeleterUserRequest{
		ID: id,
	}
}

type DescribeUserRequest struct {
	DescribeBy      DescribeBy
	DescribeByValue string
}

func NewDescribeUserRequestById(id string) *DescribeUserRequest {
	return &DescribeUserRequest{
		DescribeByValue: id,
	}
}

func NewDescribeUserRequestByUsername(username string) *DescribeUserRequest {
	return &DescribeUserRequest{
		DescribeBy:      DESCRIBEBy_USERNAME,
		DescribeByValue: username,
	}
}
